<template>
  <div>
    <!-- 合格证邮寄查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhMailInquiry"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
    <Drawer :visible.sync="visible" :title="'明细'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, getFactoryWarehouseList, gettmFiAccountType, exporturl } from '@/api/omSale/certificate/MailInquiry';
import ownerSeacher from './ownerSeacher';
import childrenModel from './childrenModel';

export default {
  name: 'MailInquiry',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      visible: false,
      isDealer: false,
      exportLoading: false
    };
  },
  components: {
    ownerSeacher,
    childrenModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.gettmFiAccountType();
    this.getFactoryWarehouseList();
  },
  methods: {
    // 加载资金类型
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({ fundsType: 0 })).data.map(x => ({ text: x.accountTypeName, value: x.accountTypeCode }));
      this.topFilterList.find(x => x.fieldName == 'accountTypeCode').itemList = res;
    },
    // 下线工厂
    async getFactoryWarehouseList() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'factoryId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    seacherFun() {
      this.isDealer = true;
    },

    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    emitcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    // 导出
    async exportHandle() {
      let json = Object.assign({}, { CODE: 'CODE_0000079' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '合格证邮寄查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '三方票号',
          fieldName: 'billNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountTypeCode',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '快递日期',
          fieldName: 'postStartDate|postEndDate',
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '邮寄状态',
          fieldName: 'postStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6065')
        },
        {
          type: 'SELECT',
          label: '下线工厂',
          fieldName: 'factoryId',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   fixed: 'left',
        //   render: (props, h) => {
        //     return (
        //       <div>
        //         <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
        //           编辑
        //         </multiuse-button>
        //       </div>
        //     );
        //   }
        // },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金融机构名称',
          dataIndex: 'bankName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
           { title: '车型',
           dataIndex: 'modelCode', 
           sorter: true },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
       
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '三方票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '邮寄人',
          dataIndex: 'postPerson',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '快递时间',
          dataIndex: 'postDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '快递公司',
          dataIndex: 'expressCompany',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '快递单号',
          dataIndex: 'expressNumber',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收件人',
          dataIndex: 'receivePerson',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收件电话',
          dataIndex: 'receivePhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮寄地址',
          dataIndex: 'receiveAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮寄状态',
          dataIndex: 'postStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6065'),
          render: p => {
            return <span>{this.createDictText(p.row.postStatus, '6065')}</span>;
          }
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 400
        }
      ];
    }
  }
};
</script>
